import type { MockMethod } from 'vite-plugin-mock';
import { RequestEnum } from '@/enum/httpEnum';
import { resultError, resultSuccess } from './_util';

let currentUserId: string | null = null;

const createFakeUserList = () => {
  return [
    {
      userId: '0',
      userName: 'Super',
      realName: 'Super',
      avatar: '',
      desc: '超级管理员',
      password: '123456',
      token:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3MzUyMjYyfQ.6_Ue7LAg2pNnY_6boHZOLnDvYkL7V4hxFOZtwYw1jk4',
      refreshToken:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3OTEzODYyfQ.DDhSNJLCA6zpbCKpF-jGELZk7uFeqVBTp7MuK-HiNyg',
      homePath: '/dashboard/analysis',
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super'
        }
      ],
      buttons: ['B_CODE1', 'B_CODE2', 'B_CODE3']
    },
    {
      userId: '1',
      userName: 'Admin',
      realName: 'Admin',
      avatar: '',
      desc: '管理员',
      password: '123456',
      token:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3MzUyMjYyfQ.6_Ue7LAg2pNnY_6boHZOLnDvYkL7V4hxFOZtwYw1jk4',
      refreshToken:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3OTEzODYyfQ.DDhSNJLCA6zpbCKpF-jGELZk7uFeqVBTp7MuK-HiNyg',
      homePath: '/dashboard/analysis',
      roles: [
        {
          roleName: 'Admin',
          value: 'admin'
        }
      ],
      buttons: ['B_CODE1', 'B_CODE2', 'B_CODE3']
    },
    {
      userId: '2',
      username: 'User',
      password: '123456',
      realName: '普通用户',
      avatar: '',
      desc: '普通用户',
      token:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3MzUyMjYyfQ.6_Ue7LAg2pNnY_6boHZOLnDvYkL7V4hxFOZtwYw1jk4',
      refreshToken:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3OTEzODYyfQ.DDhSNJLCA6zpbCKpF-jGELZk7uFeqVBTp7MuK-HiNyg',
      homePath: '/dashboard/workbench',
      roles: [
        {
          roleName: 'User',
          value: 'user'
        }
      ]
    },
    {
      userId: '99',
      username: 'test',
      password: '123456',
      realName: 'test user',
      avatar: '',
      desc: 'tester',
      token:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3MzUyMjYyfQ.6_Ue7LAg2pNnY_6boHZOLnDvYkL7V4hxFOZtwYw1jk4',
      refreshToken:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InVzZXJJZCI6MSwidXNlck5hbWUiOiJTb3liZWFuIiwidG9rZW5UeXBlIjoicmVmcmVzaFRva2VuIn0sImlhdCI6MTcxNzMwOTA2MiwiZXhwIjoxNzE3OTEzODYyfQ.DDhSNJLCA6zpbCKpF-jGELZk7uFeqVBTp7MuK-HiNyg',
      homePath: '/dashboard/workbench',
      roles: [
        {
          roleName: 'Tester',
          value: 'test'
        }
      ]
    }
  ];
};

export default [
  // 模拟登录
  {
    url: '/auth/login',
    timeout: 200,
    method: RequestEnum.POST.toLocaleLowerCase(),
    response: ({ body }: any) => {
      const { userName, password } = body;
      const checkUser = createFakeUserList().find(item => item.userName === userName && password === item.password);

      if (!checkUser) {
        return resultError('账户或者密码错误！！！');
      }
      const { userId, userName: _username, token, refreshToken, realName, desc, roles } = checkUser;
      currentUserId = userId;
      return resultSuccess({
        token,
        refreshToken
      });
    }
  },
  // 获取用户详情
  {
    url: '/auth/getUserInfo',
    timeout: 200,
    method: RequestEnum.GET.toLocaleLowerCase(),
    response: () => {
      if (!currentUserId) {
        return resultError('用户未登录！！！');
      }
      const checkUser = createFakeUserList().find(item => item.userId === currentUserId);

      if (!checkUser) {
        return resultError('用户未登录！！！');
      }
      const { userId, userName, realName, desc, roles, buttons } = checkUser;
      return resultSuccess({
        userId,
        userName,
        realName,
        roles,
        buttons,
        desc
      });
    }
  }
] as MockMethod[];
